package com.wzw.skin_code;

import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.view.ViewCompat;

import java.util.ArrayList;
import java.util.List;

public class SkinAttribute {
    private static final List<String> mAttributes = new ArrayList<>();
    private static final String TAG = SkinAttribute.class.getSimpleName();

    static {
        mAttributes.add("background");
        mAttributes.add("src");

        mAttributes.add("textColor");
        mAttributes.add("drawableLeft");
        mAttributes.add("drawableTop");
        mAttributes.add("drawableRight");
        mAttributes.add("drawableBottom");

        mAttributes.add("skinTypeface");
    }

    private List<SkinView> skinViews = new ArrayList<>();


    public void load(View view, AttributeSet attrs) {
        List<SkinPain> skinPains = new ArrayList<>();
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            //获取属性名字
            String attributeName = attrs.getAttributeName(i);
            if (mAttributes.contains(attributeName)) {
                //获取属性对应的值
                String attributeValue = attrs.getAttributeValue(i);
                if (attributeValue.startsWith("#")) {
                    continue;
                }

                int resId = 0;
                //判断前缀字符串 是否是"?"
                //attributeValue  = "?2130903043"
                if (attributeValue.startsWith("?")) {  //系统属性值

                } else {
                    //@1234564
                    resId = Integer.parseInt(attributeValue.substring(1));
                }
                if (resId != 0) {
                    SkinPain skinPain = new SkinPain(attributeName, resId, view.getClass().getName());
                    skinPains.add(skinPain);
                }
            }
        }
        if (!skinPains.isEmpty() || view instanceof TextView) {
            SkinView skinView = new SkinView(view, skinPains);
//            skinView.applySkin();
            skinViews.add(skinView);
        }
    }


    static class SkinView {
        View view;
        List<SkinPain> skinPains;

        public SkinView(View view, List<SkinPain> skinPains) {
            this.view = view;
            this.skinPains = skinPains;
        }

        public void applySkin() {
            for (SkinPain skinPair : skinPains) {

                Drawable left = null, top = null, right = null, bottom = null;
                switch (skinPair.attributeName) {
                    case "background":
                        if (view instanceof ImageView) {

                            SkinResource.getInstance().setDrawable(((ImageView) view), SkinResource.getInstance().getResName(skinPair.resId));
                        }
                        break;
                    case "src":

                        break;
                    case "textColor":
                        if (view instanceof TextView){
                            Log.d(TAG, "applySkin: "+skinPair.viewName+"---"+SkinResource.getInstance().getResName(skinPair.resId));
                            SkinResource.getInstance().setTextColor(((TextView)view),SkinResource.getInstance().getResName(skinPair.resId));
                        }
                        break;
                    case "drawableLeft":

                        break;
                    case "drawableTop":

                        break;
                    case "drawableRight":

                        break;
                    case "drawableBottom":

                        break;
                    case "skinTypeface":

                        break;
                    default:
                        break;
                }

                if (null != left || null != right || null != top || null != bottom) {
                    ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(left, top, right,
                            bottom);
                }
            }
        }


    }

    static class SkinPain {
        String attributeName;
        int resId;
        String viewName;

        public SkinPain(String attributeName, int resId, String viewName) {
            this.attributeName = attributeName;
            this.resId = resId;
            this.viewName = viewName;
        }
    }

    /**
     * 换皮肤
     */
    public void applySkin() {
        for (SkinView mSkinView : skinViews) {
            mSkinView.applySkin();
        }
    }
}
